<template>
  <el-dialog
    :title="isEdit ? '修改文章' : '添加文章'"
    :visible="isSkill"
    width="50%"
    @close="hClose"
  >
    <el-form :model="ruleForm" :rules="rules" ref="refForm">
      <el-form-item prop="title" label-width="100px" label="文章标题">
        <el-input
          v-model="ruleForm.title"
          placeholder="请输入文章标题"
        ></el-input>
      </el-form-item>
      <el-form-item prop="articleBody" label-width="100px" label="文章内容">
        <quill-editor v-model="ruleForm.articleBody"> </quill-editor>
      </el-form-item>
      <el-form-item label-width="100px" label="视频地址">
        <el-input
          v-model="ruleForm.videoURL"
          placeholder="请输入视频地址"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div style="text-align: right">
          <el-button type="primary" @click="hSave">确认</el-button>
          <el-button @click="hClose">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { add, update, detail } from '@/api/hmmm/articles'

export default {
  components: {
    quillEditor
  },
  data () {
    return {
      ruleForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    isSkill: {
      type: Boolean,
      required: true
    },
    isEdit: {
      type: Boolean,
      reuuired: true
    },
    curNodeId: {
      type: [Number, String],
      required: true
    }
  },

  methods: {
    // 关闭弹框,清空表单数据
    hClose () {
      this.$emit('update:isSkill', false)
      this.$refs.refForm.resetFields()
      this.ruleForm.videoURL = ''
    },
    async doAdd () {
      // 调用api
      const res = await add(this.ruleForm)
      console.log(res)
      // 提醒添加成功
      this.$message.success('添加成功')
      // 关闭弹框
      // 清空数据
      this.hClose()
      // 通知父组件更新数据
      this.$emit('updateManager')
    },
    async doEdit () {
      // 调用api
      await update({ ...this.ruleForm, id: this.curNodeId })

      // 提醒添加成功
      this.$message.success('修改成功')
      // 关闭弹框
      // 清空数据
      this.hClose()

      // 通知父组件更新数据
      this.$emit('updateManager')
    },
    hSave () {
      // 表单兜底校验
      this.$refs.refForm.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async getinfo () {
      const { data } = await detail(this.curNodeId)
      this.ruleForm.title = data.title
      this.ruleForm.articleBody = data.articleBody
    }
  }
}
</script>
<style scoped>
.quill-editor {
  margin-bottom: 90px;
  height: 250px;
}
</style>
